package com.airbnb.mvrx.sample.features.book.views

import android.content.Context
import android.support.v4.content.ContextCompat
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.airbnb.mvrx.sample.Utils
import com.airbnb.mvrx.sample.features.book.models.PriceModel
import com.example.mvrxstudy.R
import com.yt.ytdeep.client.dto.LessonDTO

@ModelView(autoLayout = ModelView.Size.WRAP_WIDTH_WRAP_HEIGHT)
class BookDetailFuncGridItem @JvmOverloads constructor(context: Context) : FrameLayout(context) {

    private val mImage: ImageView
    private val mTitle: TextView
    private val mPrice: TextView
    private val mContainer: LinearLayout

    init {
        inflate(context, R.layout.item_func_grid_book_detail, this)
        mImage = findViewById(R.id.iv_image)
        mTitle = findViewById(R.id.tv_title)
        mPrice = findViewById(R.id.tv_price)
        mContainer = findViewById(R.id.ll_container)
    }

    @ModelProp
    fun setImage(image: Int) {
        mImage.setImageResource(image)
    }

    @ModelProp
    fun setTitle(title: String) {
        mTitle.text = title
    }

    @ModelProp
    fun setWidth(width: Int) {
        mContainer.layoutParams.width = width
    }

    @ModelProp
    fun setPrice(priceModel: PriceModel) {
        when (priceModel.authType) {
            -1 ->
                mPrice.text = ""
            LessonDTO.LESSON_AUTHTYPE_NEED_PRICE -> {
                mPrice.setTextColor(ContextCompat.getColor(context, R.color.red))
                mPrice.text = Utils.format("￥%.2f", priceModel.authVal!!.toFloat() / 100)
            }
            LessonDTO.LESSON_AUTHTYPE_FREE -> {
                mPrice.setTextColor(ContextCompat.getColor(context, R.color.green))
                mPrice.text = "免费"
            }
            LessonDTO.LESSON_AUTHTYPE_NEED_LEARN_CARD -> {
                mPrice.setTextColor(ContextCompat.getColor(context, R.color.blue))
                mPrice.text = "学习卡"
            }
        }
    }

}